package com.youtui.common.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 鏁版嵁鍔犲瘑澶勭悊
 */
public class MessageDigestUtil {

	/**
	 * 灏嗗瓧绗︿覆杩涜sha1鍔犲瘑
	 *
	 * @param str
	 *            闇�鍔犲瘑鐨勫瓧绗︿覆
	 * @return 鍔犲瘑鍚庣殑鍐呭
	 */
	public static String sha1(String str) {
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-1");
			digest.update(str.getBytes());
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			// 瀛楄妭鏁扮粍杞崲涓�鍗佸叚杩涘埗 鏁�
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

}
